﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
  public  class MessageFormManager
    {
      static List<MessageForm> forms = new List<MessageForm>();
      static   object obj= new object();
      public static void ShowMessageForm(ShowMessageInfo si)
      {
          lock (obj)
          {
              MessageForm messageForm = new MessageForm();
              messageForm.HeightId = forms.Count;
              forms.Add(messageForm);
              messageForm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(messageForm_FormClosing);
              messageForm.ShowMessage(si);
          }
      }

     static void messageForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
      {
          lock (obj)
          {
              Form form = sender as Form;
              foreach (var v in forms)
              {
                  if (v.Bottom < form.Bottom)
                  {
                      v.SetBounds(v.Left,v.Top+15,v.Width,v.Height); 
                  }
              }
              forms.Remove(form as MessageForm);
          }
      }
    }

  public class ShowMessageInfo
  {
      /// <summary>
      /// 类型
      /// </summary>
      public string messageType;
      /// <summary>
      /// 标题
      /// </summary>
      public string messageTitle;
      /// <summary>
      /// 日期-时间
      /// </summary>
      public string messageDateTime;
      /// <summary>
      /// 消息内容
      /// </summary>
      public string messageValue;

      public Action action;

      public virtual void MessageProcess()
      {
          if (this.action != null)
              this.action();
      }

      public override bool Equals(object obj)
      {
          if (obj is ShowMessageInfo)
          {
              ShowMessageInfo tmp = (ShowMessageInfo)obj;
              if (this.messageType == tmp.messageType &&
                  this.messageTitle == tmp.messageTitle &&
                  this.messageDateTime == tmp.messageDateTime &&
                  this.messageValue == tmp.messageValue 
                  )
              {
                  return true;
              }
          }
          return false;
      }

      public override int GetHashCode()
      {
          return base.GetHashCode();
      }
  }
}
